home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE14 / TIPTRIX / OPTCASE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-07  |  2.6 KB  |  105 lines

  1. { File OPTCASE.PAS: Testing Delphi 2's code optimiser }
  2. { When there are 4 or less constants it uses dec, je sequences }
  3. case a of
  4.   1: a:= 1;
  5.   2: a:= 1;
  6.   3: a:= 1;
  7.   4: a:= 1; { This generates 4 dec <<reg>>, je sequences }
  8. end;
  9. { There must be 5 or more constants before the optimizer will use a jump table }
  10. case a of
  11.   1: a:= 1;
  12.   2: a:= 1;
  13.   3: a:= 1;
  14.   4: a:= 1;
  15.   5: a:= 1; { This generates a jump table }
  16. end;
  17. { The sequence does not have to be sorted: }
  18. case a of
  19.   1: a:= 1;
  20.   2: a:= 1;
  21.   3: a:= 1;
  22.   5: a:= 1;
  23.   4: a:= 1; { This still generates a jump table }
  24. end;
  25. { The sequence does not have to be continuous: }
  26. case a of
  27.   1: a:= 1;
  28.   2: a:= 1;
  29.   3: a:= 1;
  30.   6: a:= 1; { This still generates a jump table }
  31.   4: a:= 1;
  32. end;
  33. { When there are big steps in the sequence, the optimizer goes
  34. back to dec reg, je sequences; when the list is long it divides it
  35. into pieces by testing with jg etc }
  36. case a of
  37.   10: a:= 1;
  38.   20: a:= 1;
  39.   30: a:= 1;
  40.   40: a:= 1;
  41.   50: a:= 1;
  42. end;
  43. { It handles offsets by subtracting before jumping }
  44. case a of
  45.   10: a:= 1;
  46.   11: a:= 1;
  47.   12: a:= 1;
  48.   13: a:= 1;
  49.   14: a:= 1; { This still generates a jump table }
  50. end;
  51. { Gaps of one are included in the jump table }
  52. case a of
  53.   10: a:= 1;
  54.   12: a:= 1;
  55.   14: a:= 1;
  56.   16: a:= 1;
  57.   18: a:= 1; { This still generates a jump table }
  58. end;
  59. { When the gaps become too large (ie it would waste to much space
  60. on dummy pointers) it creates a secondary array to convert from the
  61. case-constants to jump-table indexes! The first level converts 10,
  62. 12, 14, 16 and 23 into 1, 2, 3, 4 and 5, respectively. All other
  63. numbers convert into 0. This new index is used in a jump table
  64. that only consist of 6 entries (0..5) }
  65. case a of
  66.   10: a:= 1;
  67.   12: a:= 1;
  68.   14: a:= 1;
  69.   16: a:= 1;
  70.   23: a:= 1; { Generates a two-level jump table! }
  71. end;
  72. { When the gap becomes too large even for the secondary table, it
  73. goes back to the dec, je algorithm again }
  74. case a of
  75.   10: a:= 1;
  76.   12: a:= 1;
  77.   14: a:= 1;
  78.   16: a:= 1;
  79.   100: a:= 1;
  80. end;
  81. { When you have a very long case and not all of it can be turned
  82. into a jump table, the optimizer divides it into several parts and
  83. generates a jump table for the parts that can have it. It does not
  84. seem to optimize the exact size of these parts, however... }
  85. case a of
  86.   1: a:= 1;
  87.   2: a:= 1;
  88.   3: a:= 1;
  89.   4: a:= 1;
  90.   5: a:= 1;
  91.   6: a:= 1;
  92.   7: a:= 1;
  93.   8: a:= 1;
  94.   9: a:= 1;
  95.   10: a:= 1; { There is a jump table for entries 1..10 }
  96.   11: a:= 1;
  97.   12: a:= 1;
  98.   13: a:= 1;
  99.   14: a:= 1;
  100.   15: a:= 1;
  101.   16: a:= 1;
  102.   17: a:= 1;
  103.   18: a:= 1;
  104.   100: a:= 1; { There are dec/sub, je for the rest }
  105. end;